import useBase from './useBase'

export default (egismap) => {
  const { newMarker, newImgMarker } = useBase()
  let layers = {}

  // 创建点标绘
  const createMarker = ({ lat, lng }, layerName) => {

    let elementlayer = null
    if (layerName && layers[layerName]) {
      elementlayer = layers[layerName]
      elementlayer.clear()
    } else {
      //创建标绘图层
      elementlayer = new egis.carto.ElementLayer({
        id: layerName,
        name: "标记图层",
      });
      //标绘图层添加到地图上
      egismap.addLayer(elementlayer);
      if (layerName) layers[layerName] = elementlayer
    }

    //构造一个空的点元素对象
    var pointElement = newMarker({ lat, lng });
    elementlayer.add(pointElement)
    // console.log(elementlayer, pointElement)

    return pointElement
  }

  // 创建点标绘
  const createImgMarker = ({ lat, lng }, layerName) => {
    let elementlayer = null
    console.log(lat, lng, layerName)
    if (layerName && layers[layerName]) {
      elementlayer = layers[layerName]
      elementlayer.clear()
    } else {
      //创建标绘图层
      elementlayer = new egis.carto.ElementLayer({
        id: layerName,
        name: "标记图层",
      });
      //标绘图层添加到地图上
      console.log(lat, lng, layerName,egismap)
      egismap.addLayer(elementlayer);
      if (layerName) layers[layerName] = elementlayer
    }

    //构造一个空的点元素对象
    var pointElement = newImgMarker({ lat, lng });
    elementlayer.add(pointElement)
    // console.log(elementlayer, pointElement)

    return pointElement
  }

  return {
    createMarker,
    createImgMarker
  }
}